#!/bin/bash
#
# Init file for Lookup Service 
# chkconfig: 2345 90 20
# description: Lookup Service server. Used if using rpm to install
#
#
# Source function library.
. /etc/rc.d/init.d/functions

SHORT_NAME=simple-lookup-service
LONG_NAME="Simple Lookup Service"
VERSION="0.1"

#LS variables
HOME_PREFIX=/etc/opt/$SHORT_NAME
DIST_PREFIX=/opt/$SHORT_NAME
BINDIR=$DIST_PREFIX/bin
CONFDIR=$HOME_PREFIX
RUNDIR=/var/run/$SHORT_NAME
LOGDIR=/var/log/$SHORT_NAME
QDATADIR=$DIST_PREFIX/data
LOCK_FILE=/var/lock/subsys/$SHORT_NAME
USER=lookup
GROUP=lookup
STOP_TIMEOUT=10
RESTART_DELAY=5
PIDFILE=${RUNDIR}/${SHORT_NAME}.pid
JARFILE=$DIST_PREFIX/target/$SHORT_NAME-server-$VERSION.one-jar.jar
LOOKUPSERVICE_CMD="$BINDIR/startServer.sh $PIDFILE $JARFILE -c $CONFDIR/ -l $CONFDIR/log4j.properties -d $QDATADIR > $LOGDIR/$SHORT_NAME.out 2>&1 &"

#functions
start(){
    echo -n $"Starting ${SHORT_NAME}: "
    echo $LOOKUPSERVICE_CMD
    daemon --pidfile=${PIDFILE} --user=${USER} $LOOKUPSERVICE_CMD
    RETVAL=$?
    echo
    [ $RETVAL = 0 ] && touch ${LOCK_FILE}
    return $RETVAL
}

stop(){
    echo -n $"Stopping ${SHORT_NAME}: "
	killproc -p ${PIDFILE} -d ${STOP_TIMEOUT} java
	RETVAL=$?
	echo
	[ $RETVAL = 0 ] && rm -f ${LOCK_FILE} ${PIDFILE}
}

#handle arguments
ERROR=0
ARGV="$@"
if [ -z "$ARGV" ] ; then 
    ARGS="help"
fi

for ARG in $@ $ARGS
do
    case $ARG in
    start)
        start
	;;
    stop)
	    stop
	;;
    restart)
    	stop
    	sleep $RESTART_DELAY
    	start
	;;
	status)
	    status -p ${PIDFILE} ${SHORT_NAME}
	;;
    *)
	echo "usage: $0 (start|stop|restart|help)"
	cat <<EOF

start      - start ${LONG_NAME}
stop       - stop ${LONG_NAME}
restart    - restart ${LONG_NAME} if running by sending a SIGHUP or start if 
             not running
status     - status of ${LONG_NAME}
help       - this screen

EOF
	ERROR=2
    ;;

    esac

done

exit $ERROR
